package cn.wolfcode.crm.service.Impl;

import cn.wolfcode.crm.domain.CustomerTransferHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferHistoryMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
@Transactional
public class CuotomerTransferHistorySreviceImpl implements ICustomerTransferHistoryService{

    @Autowired
    private CustomerTransferHistoryMapper customerTransferHistoryMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public void save(CustomerTransferHistory customerTransferHistory) {

        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        customerTransferHistory.setReansUser(employee);
        customerTransferHistory.setTransTime(new Date());
        customerTransferHistoryMapper.insert(customerTransferHistory);
        customerMapper.updateSeller(customerTransferHistory.getCustomer().getId(),
                customerTransferHistory.getNewSeller().getId());
    }


    @Override
    public void update(CustomerTransferHistory customerTransferHistory) {
        customerTransferHistoryMapper.updateByPrimaryKey(customerTransferHistory);
    }

    @Override
    public CustomerTransferHistory get(Long id) {
        return customerTransferHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransferHistory> list() {
        return customerTransferHistoryMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<CustomerTransferHistory> customerTransferHistorys = customerTransferHistoryMapper.selectForList(qo);
        return new PageInfo(customerTransferHistorys);
    }
}
